<?php
/**
 * @package pmapi
 * @copyright Copyright (c)2014 Enrico Fagnoni
 * @license GNU General Public License version 3, or later
 *
 * @since 1.0.0
 */
 
defined('_JEXEC') or die;

/**
 * @param	array	A named array
 * @return	array
 */
function PmapiBuildRoute(&$query)
{
	$segments = array();

    if (isset($query['format'])) {
        $segments[] = $query['format'];
        unset($query['format']);
    }   
    if (isset($query['view'])) {
        $segments[] = $query['view'];
        unset($query['view']);
    }
	if (isset($query['id'])) {
		$segments[] = $query['id'];
		unset($query['id']);
	}


	return $segments;
}

/**
 * @param	array	A named array
 * @param	array
 *
 * Formats:
 *
 * index.php?/pmapi/task/id/Itemid
 *
 * index.php?/pmapi/id/Itemid
 */
function PmapiParseRoute($segments)
{
	$vars = array();
    
	// view is always the first element of the array
	$count = count($segments);
    
    if ($count==3)
    {
        $vars['format'] = $segments[0];
        $vars['view'] = $segments[1];
        $vars['id'] = $segments[2];
    } elseif ($count==2)
	{
        $vars['format'] = $segments[0];
	    $vars['view'] = $segments[1];
	} elseif ($count==1)
    {
        $vars['format'] = $segments[0];
    }

	return $vars;
}
/*
 * 
function PmapiParseRoute($segments)
{
    $vars = array();
    
    // view is always the first element of the array
    $count = count($segments);
    
    if ($count)
    {
        $count--;
        $segment = array_pop($segments) ;
        if (is_numeric($segment)) {
            $vars['id'] = $segment;
        }
        else{
            $count--;
            $vars['task'] = array_pop($segments) . '.' . $segment;
        }
    }

    if ($count)
    {   
        $vars['task'] = implode('.',$segments);
    }
    return $vars;
}
 */
